// pages/print/papersEdit/papersEdit.js
var utils = require("../../../utils/utils.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        serviceSubtypeId: 0,
        src: '',
        printSrc: '',
        width: '295',
        height: '413',
        left: '227.5',
        num: 1,
        List: {},
        temps: [],
        chooseTempIndex: 0,
        chooseClother: '',
        imgId: 0,
        color: 'red',
        pid: '',
        showIos:false
    },
    btnConfirm() {
        var that = this;
        if (!utils.is_define(that.data.src)) {
            utils.toast('证件处理失败,请返回重新上传');
            return false;
        }
        if (wx.getStorageSync('machineQrCode')) {
            wx.redirectTo({
                url: '../preview/preview?code=' + that.data.code + '&name=' + that.data.name + '&pageNum=1&imgsrc=' + that.data.printSrc + '&tagFloor=' + that.data.tagFloor + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&clothesId=' + that.data.imgId + '&certificatesPhoto=true',
            })
        } else {
            wx.navigateTo({
                url: '/pages/print/near/near?from=card',
            })
        }

    },
    btnConfirmC() {
        var that = this;
        wx.showModal({
            title: '电子版证件照提示',
            content: '电子版证件照仅供下载，无法打印，请知悉',
            showCancel: true,
            confirmText: '确定',
            cancelText: '重新选择',
            success(res) {
                if (res.confirm) {

                    var clothes = that.data.imgId == '0' ? '-1' : '1'
                    var money = that.data.imgId == '0' ? that.data.money : parseFloat(that.data.clothesMoney) + parseFloat(that.data.money)
                    wx.navigateTo({
                        url: '/pages/print/pay/pay?serviceSubtypeId=' + that.data.serviceSubtypeId + '&price=' + parseFloat(money).toFixed(2) + '&fileName=' + that.data.name + '&fileUrlSingle=' + that.data.src + '&fileUrl=' + that.data.printSrc + '&clothes=' + clothes + '&orderType=521'
                    })
                }
            }
        })
    },
    choosemb(e) {
        var that = this;
        var imgId = e.currentTarget.dataset.imgid;
        var index = e.currentTarget.dataset.index;
        if (imgId == that.data.imgId) {
            return false;
        }
        that.setData({
            imgId: imgId,
            intoView: 'view' + index
        })
        // wx.showLoading({
        //     title: '处理中',
        //     mask: true
        // })
        that.selectComponent("#loading").showLoadingCus('处理中...');
        that.photoBeautify(that.data.url);
    },
        //美颜
    photoBeautify(url){
        var that = this;
        utils.request('convert/photoBeautify',{url:url}, function (res) {
            if (res.data.code == 200) {
              that.makingCard(res.data.data.url)
            } else {
              that.makingCard(url)
            }
          })

    },
    choosetempIndex(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            chooseTempIndex: index,
            intoView: 'viewshi'
        })
    },
    saveImg() { },
    lessen() {
        var num = this.data.num;
        if (num == 1) {
            utils.toast('最小份数为1')
            return false;
        }
        num--;
        this.setData({
            num: num
        })
    },
    add() {
        var num = this.data.num;
        num++;
        this.setData({
            num: num
        })
    },
    //获取证件照正装模板
    getMOdel() {
        var that = this;
        utils.request('idPhoto/clothesLists', {}, function (res) {
            // return false;
            if (res.data.code == 200) {
                // for (var i in res.data.data) {
                //   array.push(res.data.data[i])
                // }
                that.setData({
                    temps: res.data.data,
                    gatewayCode: res.data.data.gatewayCode
                })
                if (utils.is_define(that.data.code) && that.data.code == 'idPhoto_weddingPhoto') {
                    that.makingCardWedding();
                } else {
                    that.photoBeautify(that.data.url);
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    //制作证件照
    makingCard(url) {
        var that = this;
        // wx.showLoading({
        //     title: '证件照处理中',
        //     mask: true
        // })
        that.selectComponent("#loading").showLoadingCus('处理中...');
        var data = {
            picUrl:url,
            other: that.data.pid ? '1' : '-1',
            itemId: that.data.serviceSubtypeId || that.data.pid,
            clothesId: that.data.imgId || '',
            gatewayCode: that.data.gatewayCode,
            photoKey: that.data.photoKey
        }
        utils.request('idPhoto/cutCheckPic', data, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                that.setData({
                    src: res.data.data.img[0][that.data.color].img,
                    printSrc: res.data.data.img[0][that.data.color].imgPrint,
                    name: res.data.data.img[0][that.data.color].name,
                    List: res.data.data.img,
                    money: res.data.data.salePrice,
                    clothesMoney: res.data.data.clothesPrice,
                    photoKey: res.data.data.photoKey || ''
                })
            } else {
                wx.showModal({
                    title: '处理失败',
                    content: res.data.msg,
                    showCancel: false,
                    confirmText: '重新选择',
                    complete: (res) => {
                        if (res.confirm) {
                            wx.navigateBack()
                        }
                    }
                })
            }
        })
    },
    //   结婚照
    makingCardWedding() {
        var that = this;
        // wx.showLoading({
        //     title: '证件照处理中',
        //     mask: true
        // })
        that.selectComponent("#loading").showLoadingCus('处理中...');
        var data = {
            picUrl: that.data.url,
            rgb: "200, 37, 40"
        }
        utils.request('idPhoto/cutWedding', data, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                that.setData({
                    src: res.data.data.img,
                    printSrc: res.data.data.imgPrint
                    // payTypeList: res.data.data.payTypeList
                })
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    imgBase(e) {
        var color = e.currentTarget.dataset.color;
        for (const key in this.data.List[0]) {
            if (color == key) {
                this.setData({
                    name: this.data.List[0][color].name,
                    src: this.data.List[0][color].img,
                    printSrc: this.data.List[0][color].imgPrint,
                    color: color
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.setNavigationBarTitle({
            title: options.title
        })
        var str = qq.getSystemInfoSync().system
        if (str.indexOf('iOS') > -1) {
            that.setData({
                showIos: false
            })
        } else {
            that.setData({
                showIos: true
            })
        }
        that.setData({
            pageNum: 1,
            code: options.code ? options.code : '',
            url: options.url,
            tagFloor: options.tagFloor,
            serviceMainId: options.serviceMainId,
            title: options.title,
            pid: options.pid ? options.pid : '',
            serviceSubtypeId: options.serviceSubtypeId ? options.serviceSubtypeId : options.pid
        })
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68rpx',
                        img: options.imgsrc,
                        tempFilePaths: []
                    })
                } else {
                    that.setData({
                        pdbottom: '40rpx',
                        img: options.imgsrc,
                        tempFilePaths: []
                    })
                }
            },
        })
        that.getMOdel();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})